package com.swsc.solarverse.common.mybatis;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @Author chencl
 * @Date 2022/1/10 13:19
 * @Version 1.0
 * @Description SelectForUpdateMethod
 */
public class SelectForUpdateMethod extends AbstractMethod {
    private static final String SQL_TEMPLATE = "<script>%s SELECT %s FROM %s %s %s for update\n</script>";
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        SqlSource sqlSource = languageDriver.createSqlSource(configuration,
                String.format(
                        SQL_TEMPLATE,
                        sqlFirst(),
                        sqlSelectColumns(tableInfo, true),
                        tableInfo.getTableName(),
                        sqlWhereEntityWrapper(true, tableInfo),
                        sqlComment()
                ),
                modelClass);
        return this.addSelectMappedStatementForTable(mapperClass, "selectOneForUpdate", sqlSource, tableInfo);
    }
}
